package cn.briarbear.myblog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.sql.DataSource;

/**
 * Spring Security配置类
 *
 * @auther Val1ant
 * @blog http://www.val1ant.xin
 * @create 2018-05-11-9:23
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private DataSource dataSource;

    @Qualifier("adminUserService")
    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 密码编码器
     * @return
     */
    @Bean
    PasswordEncoder passwordEncoder(){
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.authorizeRequests()
                .antMatchers("/", "/blog", "/about", "/adminlogin").permitAll()
                .antMatchers("/static/**").permitAll()
//                .anyRequest().authenticated()
                .antMatchers("/admin/**", "/admin_user/**").hasRole("ADMIN")
                .and()
                .formLogin()
//                    .usernameParameter("username")
//                    .passwordParameter("password")
                    .loginPage("/adminlogin")
                    .failureUrl("/adminlogin")
                    .defaultSuccessUrl("/admin")
                    .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout","GET"));
//                .logout().logoutSuccessUrl("/").permitAll();

//        http.csrf().disable();
        http.csrf().ignoringAntMatchers("/admin/**"); //忽略/admin/路径下的CSRF保护
        http.csrf().ignoringAntMatchers("/blog/**"); //忽略/blog/路径下的CSRF保护
        http.csrf().ignoringAntMatchers("/about/**"); //忽略/blog/路径下的CSRF保护
        http.csrf().ignoringAntMatchers("/love/**"); //忽略/blog/路径下的CSRF保护

    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        内存用户方式
//        auth.inMemoryAuthentication().withUser("xzp")
//                .password("123").roles("ADMIN");
        //JDBC方式
//        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username,password,true"+"from admin where username = ?");
       //JPA方式
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }


}
